//
//  DLShowImageVC.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/12/11.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLShowImageVC.h"

@interface DLShowImageVC ()
@property(nonatomic,strong)UIImageView *showImgV;
@property(nonatomic,strong)UIScrollView *mainScrollV;
@property (nonatomic, strong) UIButton *savePicBtn;

@end

@implementation DLShowImageVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    kWeakSelf;
    [self.view setBackgroundColor:[UIColor blackColor]];
    UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.mainScrollV = mainScrollView;
    [self.view addSubview:mainScrollView];
    //图片显示
    _showImgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0)];
    [mainScrollView addSubview:_showImgV];
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes)];
    _showImgV.userInteractionEnabled = YES;
    [_showImgV addGestureRecognizer:tapGes];
    
    if (self.showImgUrl && self.showImgUrl.length>0) {
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [hud show:YES];
        
        [_showImgV sd_setImageWithURL:[NSURL URLWithString:self.showImgUrl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            
            if (image == nil) {
            
                [hud hide:YES];

                kShowRequestMsg(@"图片加载错误！");
                self.savePicBtn.userInteractionEnabled = NO;
                return ;
            }
            
            [weakSelf.showImgV resetHeight:kScreenWidth*(image.size.height/image.size.width)];
            weakSelf.showImgV.center = weakSelf.view.center;
            [weakSelf.mainScrollV setContentSize:CGSizeMake(kScreenWidth, MAX(kScreenHeight, weakSelf.showImgV.height))];
            if (weakSelf.showImgV.height>kScreenHeight) {
                [weakSelf.showImgV resetY:0];
            }
            [hud hide:YES];
        }];
    } else if (self.showImg) {
        
        [_showImgV setImage:self.showImg];
        [_showImgV resetHeight:kScreenWidth*(self.showImg.size.height/self.showImg.size.width)];
        _showImgV.center = self.view.center;
        [self.mainScrollV setContentSize:CGSizeMake(kScreenWidth, MAX(kScreenHeight, self.showImgV.height))];
        if (self.showImgV.height>kScreenHeight) {
            [self.showImgV resetY:0];
        }
    }
    //关闭
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setFrame:CGRectMake(kScreenWidth-60, 30, 50, 30)];
    [closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
    closeBtn.layer.cornerRadius = 15.0;
    [closeBtn setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]];
//    [self.view addSubview:closeBtn];
    [closeBtn addTarget:self action:@selector(dismissSelf) forControlEvents:UIControlEventTouchUpInside];
    
    //保存图片
    UIButton *savePicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [savePicBtn setFrame:CGRectMake(kScreenWidth-60, kScreenSafeAreaHeight-60, 50, 50)];
    savePicBtn.layer.cornerRadius = 6.0;
    [savePicBtn setImage:[UIImage imageNamed:@"savePic"] forState:UIControlStateNormal];
    savePicBtn.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    [savePicBtn setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]];
//    [self.view addSubview:savePicBtn];
    [savePicBtn addBlock4Tap:^(UIView *view) {
        UIImageWriteToSavedPhotosAlbum(weakSelf.showImgV.image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
    }];
    self.savePicBtn = savePicBtn;
}
- (void) tapGes {
    
    [self dismissSelf];
}
- (void) dismissSelf {
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
    kShowRequestMsg(@"图片保存成功！");
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
